import utils from "./utils";
export default {
  onDrawn(drawnFn) {
    // if (!drawnFn || typeof drawnFn != "function") drawnFn = function() {};
    // this.drawnFn = drawnFn;
  },
  onMousedown(opt, canvas) {},
  onMousemove(opt, canvas) {},
  onMouseup(opt, canvas) {},
  draw(canvas, drawObj, allOptions) {
    var fontSize = 18 / canvas.getZoom();

    if ((!drawObj.pointers || drawObj.pointers.length == 0) && drawObj.bindId) {
      var options = allOptions.filter((o) => o.id == drawObj.bindId);
      if (options.length > 0) {
        options = options[0];
        drawObj.pointers = [...options.pointers];
      }
    }

    let top = drawObj.pointers[0].y - fontSize - 2 / canvas.getZoom();
    let left = drawObj.pointers[0].x;
    var text = (drawObj.text || "").toString();

    var drawOptions ={}

    if (drawObj.fabricObj) {
      drawOptions={
        text: text,
        fontSize: fontSize,
        top: top,
        left: left,
        strokeWidth: 1 / canvas.getZoom(),
      }
      drawObj.fabricObj.set(utils.combineOptions(drawOptions,drawObj.drawOptions));
      canvas.requestRenderAll();
    } else {
      drawOptions={
        top: top,
        left: left,
        fontSize: fontSize,
        fontFamily: "宋体",
        fill: "rgb(0,0,0,0)",
        strokeWidth: 1 / canvas.getZoom(),
        stroke: "rgba(100,200,200,0.8)",
        selectable: false,
      }
      drawObj.fabricObj = new fabric.Text(text,utils.combineOptions(drawOptions,drawObj.drawOptions) );
      canvas.add(drawObj.fabricObj);
    }
    drawOptions=null;
  },
  remove(canvas, drawObj) {
    if (drawObj.fabricObj){
      canvas.remove(drawObj.fabricObj);
      //drawObj.fabricObj.dispose();
      drawObj.fabricObj = null;
    }
  },
};
